package learn.up.collections;

import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author NANUTO
 * @date 2017年11月21日 下午11:45:35
 */
public class Collectioins {
	
	public interface Expression<E> {
		/**
		 * 过滤条件
		 * @return
		 */
		boolean filtrate(E e);
	}
	
	public static <E> int count(Collection<E> items, Expression<E> expression) {
		int count = 0;
		for (E e : items) {
			if (expression.filtrate(e))
				count ++;
		}
		return count;
	}
	
	public static <E> Collection<E> findConformity(Collection<E> items, Expression<E> expression) {
		Iterator<E> iterator = items.iterator();
		while (iterator.hasNext()) {
			if (! expression.filtrate(iterator.next()))
				iterator.remove();
		}
		return items;
	}
	
	/**
	 * 是否所有都满足
	 * @param items
	 * @param expression
	 * @return
	 */
	public static <E> boolean all(Collection<E> items, Expression<E> expression) {
		return count(items, expression) == items.size();
	}
	
	public static <E> boolean any(Collection<E> items, Expression<E> expression) {
		for (E e : items) {
			if (expression.filtrate(e))
				return true;
		}
		return false;
	}
	
	@SafeVarargs
	public static <E> boolean addAll(Collection<? super E> c, E... e) {
		boolean result = false;
		for (E e2 : e) {
			result |= c.add(e2);
		}
		return result;
	}
	
	public static void main(String[] args) {
		System.err.println(0 | 1);
		System.out.println(false | true);
	}
}
